
package com.by.yz.activity;

import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.adapter.Adapter;
import com.by.yz.common.MFGT;
import com.by.yz.custom.CircleImageView;
import com.by.yz.custom.CircleNetworkImageView;
import com.by.yz.custom.TechnologyTeamDividerItemDecoration;
import com.by.yz.entity.Account;
import com.by.yz.entity.Click;
import com.by.yz.entity.Comment;
import com.by.yz.entity.Doctorbean;
import com.by.yz.entity.TopicInfo;
import com.by.yz.entity.User;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.ClickRESTtful;
import com.by.yz.loginUtil.CommentRESTFul;
import com.by.yz.loginUtil.TopicInfoRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.IsNetWork;
import com.by.yz.util.Start_End_times;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Handler;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

/**
 * Created by John on 2016/10/13.
 */
public class CurrentTopicsActivity extends BaseActivity implements View.OnClickListener {

    RecyclerView recyclerView;
    List<Comment> commentList = new ArrayList<Comment>();
    SimpleStringRecyclerViewAdapter asd;
    private static ArrayList<String> datas = new ArrayList<String>();

    TextView topictitle;//话题标题
    TextView creater;//话题发表人
    TextView topiccontent;//话题内容
    TextView createat;//话题发表时间
    CircleNetworkImageView creater_img;//发表人头像
    RelativeLayout dianzanlayout;//点赞布局
    RelativeLayout commentlayout;//评论布局
    TextView dianzan;//点赞
    TextView disscuss;//评论
    ImageView dianzanimg;//点赞图片
    TopicInfo topicInfo;//评论图片
    Boolean isClick = false;
    Button diss;

    NetworkImageView topicimg1, topicimg2, topicimg3;

    Click click;
    List<Click> clickList = new ArrayList<Click>();//点赞列表
    Account account;//获取当前账户
    static Comment comment;
    private LinearLayout disscuss_layout,disscuss_layout1;//评论布局
    private LinearLayout no_comment;
    TextView dis_cancel;//取消评论
    TextView dis_release;//发表评论
    EditText diss_comment;//评论输入框
    Integer dcount;//点赞数
    Integer ccount;//评论数数
    private LinearLayout Lmain;
    List<TopicInfo> topicInfoList = new ArrayList<TopicInfo>();
    private static UserTest userTest;
    private  Account useraccount;
    private Start_End_times set;
    private UserTest user;
    int page=0;
    boolean isComment;
    IsNetWork isNetWork;
    private ImageView topBtn;
    private ScrollView sc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("onCreate","onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_topic);
        setToolbarTitle("话题详情");
        asd = new SimpleStringRecyclerViewAdapter(this, commentList);;
        commentList.clear();
        topicInfoList.clear();
        datas.clear();
        initView();
        initDatas();
    }

    @Override
    protected void onStart() {
        Log.e("onStart","onStart");
//        commentList.clear();
//        topicInfoList.clear();
//        datas.clear();
        super.onStart();
        if(isNetWork.isNetworkAvailable(this)) {
//            commentList.clear();
//            initView();
//            initDatas();
        }else {
            //无网络连接情况
            new AlertDialog.Builder(this)
                    .setTitle("提示!")
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setMessage("检测到您还没开启网络，请开启")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("开启",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    startActivity(new Intent(
                                            Settings.ACTION_WIRELESS_SETTINGS));// 进入无线网络配置界面
                                }
                            }).show();
        }
    }

    private void initView()
    {
        if(isNetWork.isNetworkAvailable(this)) {
            Log.e("initView", "initView");
            Intent intent = this.getIntent();
            topicInfo = (TopicInfo) intent.getSerializableExtra("currentopic");
            isComment = intent.getBooleanExtra("isComment", false);
            Log.e("isComment", isComment + "");
            disscuss_layout = (LinearLayout) findViewById(R.id.disscuss_layout);
            disscuss_layout1 = (LinearLayout) findViewById(R.id.disscuss_layout1);
            diss_comment = (EditText) findViewById(R.id.diss_comment);
            if (isComment) {
                commentStart();
            }
            sc = (ScrollView) findViewById(R.id.sc);
            topBtn = (ImageView) findViewById(R.id.btn_toTop);
            topBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    sc.post(new Runnable() {

                        @Override
                        public void run() {
                            sc.post(new Runnable() {
                                public void run() {
                                    // 滚动至顶部
                                    sc.fullScroll(ScrollView.FOCUS_UP);
                                }
                            });
                        }
                    });

                }
            });
            topictitle = (TextView) findViewById(R.id.report_title_textview);
            topiccontent = (TextView) findViewById(R.id.topic_content_textview);
            creater = (TextView) findViewById(R.id.report_person_name_textview);
            createat = (TextView) findViewById(R.id.report_time_textview);
            creater_img = (CircleNetworkImageView) findViewById(R.id.report_person_image);
            no_comment = (LinearLayout) findViewById(R.id.no_comment);
            Lmain = (LinearLayout) findViewById(R.id.main);
            Lmain.setOnClickListener(this);

            topictitle.setText(topicInfo.getTitle());
            topiccontent.setText(topicInfo.getContent());
            String time = topicInfo.getCreateat().toString();
            String time1 = time.replace("T", " ");
            String time2 = time1.substring(0, 16);
            createat.setText(time2);
            creater_img.setDefaultImageResId(R.mipmap.headimg);
            creater_img.setErrorImageResId(R.mipmap.headimg_error);
            UserRESTFul userRESTFul = new UserRESTFul();
            userRESTFul.getUser(topicInfo.getCreater(), new UserRESTFul.UserPOSTCallBack() {
                @Override
                public void ok(JSONObject response) throws JSONException {
                    user = ToJSonObject.jsonToBean(response.toString(), UserTest.class);
                    creater.setText(user.getnickName());
                    if (user.getHeadimg() != null) {
                        creater_img.setImageUrl(user.getHeadimg(),App.imageLoader);
                    }
                }

                @Override
                public void error(String response) { }
             });
           // creater.setText(topicInfo.getCreater());
            creater_img.setOnClickListener(this);
            dianzanlayout = (RelativeLayout) findViewById(R.id.dianzan_layout);
            dianzanlayout.setOnClickListener(this);
            commentlayout = (RelativeLayout) findViewById(R.id.comment_layout);
            commentlayout.setOnClickListener(this);
            dianzan = (TextView) findViewById(R.id.dianzan_count);
            disscuss = (TextView) findViewById(R.id.comment_count);
            dcount = topicInfo.getCount();
            ccount = topicInfo.getDisscusscount();
            dianzan.setText(dcount.toString());
            disscuss.setText(ccount.toString());
            dianzanimg = (ImageView) findViewById(R.id.imageView1);
            dis_release = (TextView) findViewById(R.id.dis_release);
            dis_release.setOnClickListener(this);
            diss=(Button)findViewById(R.id.diss_button);
            diss.setOnClickListener(this);
            topicimg1 = (NetworkImageView) findViewById(R.id.topicimg1);
            topicimg1.setOnClickListener(this);
            topicimg2 = (NetworkImageView) findViewById(R.id.topicimg2);
            topicimg2.setOnClickListener(this);
            topicimg3 = (NetworkImageView) findViewById(R.id.topicimg3);
            topicimg3.setOnClickListener(this);

            String topicimgpath = topicInfo.getTopicimage();
            if(topicimgpath!=null && !topicimgpath.equals("")) {
                String[] imagepath = topicimgpath.split(",");
                if (imagepath.length == 1){
                    topicimg1.setDefaultImageResId(R.mipmap.health_passage_default);
                    topicimg1.setErrorImageResId(R.mipmap.health_topic_error);
                    topicimg1.setImageUrl(imagepath[0].toString(), App.imageLoader);
                    topicimg1.getLayoutParams().height = 1000;
                    datas.add(0,imagepath[0].toString());
                    topicimg2.setVisibility(View.GONE);
                    topicimg3.setVisibility(View.GONE);
                }else if (imagepath.length == 2){
                    topicimg1.setDefaultImageResId(R.mipmap.health_passage_default);
                    topicimg1.setErrorImageResId(R.mipmap.health_topic_error);
                    topicimg1.setImageUrl(imagepath[0].toString(), App.imageLoader);
                    topicimg1.getLayoutParams().height = 500;
                    topicimg2.setDefaultImageResId(R.mipmap.health_passage_default);
                    topicimg2.setErrorImageResId(R.mipmap.health_topic_error);
                    topicimg2.setImageUrl(imagepath[1].toString(), App.imageLoader);
                    topicimg2.getLayoutParams().height = 500;
                    datas.add(0,imagepath[0].toString());
                    datas.add(1,imagepath[1].toString());
                    topicimg3.setVisibility(View.GONE);
                }else if (imagepath.length == 3) {
                    topicimg1.setDefaultImageResId(R.mipmap.health_passage_default);
                    topicimg1.setErrorImageResId(R.mipmap.health_topic_error);
                    topicimg1.setImageUrl(imagepath[0].toString(), App.imageLoader);
                    topicimg2.setDefaultImageResId(R.mipmap.health_passage_default);
                    topicimg2.setErrorImageResId(R.mipmap.health_topic_error);
                    topicimg2.setImageUrl(imagepath[1].toString(), App.imageLoader);
                    topicimg3.setDefaultImageResId(R.mipmap.health_passage_default);
                    topicimg3.setErrorImageResId(R.mipmap.health_topic_error);
                    topicimg3.setImageUrl(imagepath[2].toString(), App.imageLoader);
                    datas.add(0,imagepath[0].toString());
                    datas.add(1,imagepath[1].toString());
                    datas.add(2,imagepath[2].toString());
                }
            } else {
                topicimg1.setVisibility(View.GONE);
                topicimg2.setVisibility(View.GONE);
                topicimg3.setVisibility(View.GONE);
            }

            recyclerView = (RecyclerView) findViewById(R.id.topic_comment_listview);
            recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
            recyclerView.setAdapter(asd);
            recyclerView.addItemDecoration(new TechnologyTeamDividerItemDecoration(this, TechnologyTeamDividerItemDecoration.VERTICAL_LIST));
        }else {
            //无网络连接情况
            new AlertDialog.Builder(this)
                    .setTitle("提示!")
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setMessage("检测到您还没开启网络，请开启")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("开启",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    startActivity(new Intent(
                                            Settings.ACTION_WIRELESS_SETTINGS));// 进入无线网络配置界面
                                }
                            }).show();
        }
    }
    private void initDatas() {
        Log.e("initDatas","initDatas");
        setDianzan();
        getAllComment();
    }

    //获取全部评论
    public void getAllComment() {
        Log.e("getAllComment","getAllComment");
        final TopicInfo topicInfo = (TopicInfo) getIntent().getSerializableExtra("currentopic");
        Log.e("topicInfo.getId", String.valueOf(topicInfo.getId()));
        CommentRESTFul commentRESTFul = new CommentRESTFul();
        commentRESTFul.getTopicComments(topicInfo.getId(), page, new CommentRESTFul.MyGetCallBack() {
            @Override
            public void ok(JSONArray response) throws JSONException {
                page++;
                if(response.length() == 0){
                    no_comment.setVisibility(View.VISIBLE);
                }else{
                    no_comment.setVisibility(View.GONE);
                }
                for (int i = 0; i < response.length(); i++) {
                    JSONObject jsonObject = response.getJSONObject(i);
                    comment = ToJSonObject.jsonToBean(jsonObject.toString(), Comment.class);
                    commentList.add(comment);
                }
                asd.notifyDataSetChanged();
            }

            @Override
            public void error(String error) {
                commentList = null;
            }
        });
    }

    //点赞的初始化事件
    public void setDianzan(){
        AccountRESTFul accountRESTFul=new AccountRESTFul();
        accountRESTFul.get(new AccountRESTFul.IhttpCb() {
            @Override
            public void ok(JSONObject response) {
                JSONObject jsonObject=response;
                account=ToJSonObject.jsonToBean(jsonObject.toString(),Account.class);
                ClickRESTtful clickRESTtful=new ClickRESTtful();
                clickRESTtful.getClick(new ClickRESTtful.MyGetCallBack() {
                    @Override
                    public void ok(JSONArray response) throws JSONException {
                        Click getclick;
                        for(int i=0;i<response.length();i++){
                            JSONObject jsonObject = response.getJSONObject(i);
                            getclick = ToJSonObject.jsonToBean(jsonObject.toString(), Click.class);
                            clickList.add(i,getclick);
                            if (topicInfo.getId().equals(clickList.get(i).getTopic())) {
                                if(clickList.get(i).getClicker().toString().equals(account.getLogin())) {
                                    dianzanimg.setImageResource(R.mipmap.community_sign_up_press);
                                    dianzanlayout.setClickable(false);
                                    break;
                                }else{
                                    dianzanimg.setImageResource(R.mipmap.community_sign_up_normal);
                                }
                                continue;
                            } else {
                                continue;
                            }
                        }
                    }

                    @Override
                    public void error(String error) {
                        Log.e("ylj","获取点赞信息失败"+error);
                    }
                });
            }

            @Override
            public void error(String error) {
                Log.e("PCF-account-error","授权失败"+error);
            }
        });
    }

    //将点赞的信息保存在数据库
    public void dianzanSuccess(){
        click = new Click();
        try {
            click.setClicker(account.getLogin());
            click.setIsclick(true);
            click.setTopic(topicInfo.getId());
            JSONObject jsonObject = ToJSonObject.beanToJSONObejct(click);
            ClickRESTtful clickRESTtful = new ClickRESTtful();
            clickRESTtful.postClick(jsonObject, new ClickRESTtful.MyPostCallBack() {
                 @Override
                 public void ok(JSONObject response) throws JSONException {
                     Log.e("ssss", response.toString());
                     dcount = topicInfo.getCount();
                     dcount += 1;
                     dianzanimg.setImageResource(R.mipmap.community_sign_up_press);
                    dianzan.setText(dcount.toString());
                    topicInfo.setCount(dcount);
                    putTopic();
                }

                 @Override
                 public void error(String error) {
                Log.e("eeeee", "点赞失败" + error);
            }
             });
        }catch (Exception e){
            Log.e("ssss",""+ e);
        }
    }
    //点赞成功
    public void putTopic(){
        JSONObject jsonObject = ToJSonObject.beanToJSONObejct(topicInfo);
        TopicInfoRESTFul topicful = new TopicInfoRESTFul();
        topicful.putTopicInfo(jsonObject, new TopicInfoRESTFul.MyPostCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                dianzanlayout.setClickable(false);
                Toast.makeText(CurrentTopicsActivity.this, "点赞成功！", Toast.LENGTH_SHORT).show();
                commentList.clear();
            }
            @Override
            public void error(String error) {
                Log.e("失败", "点赞失败" + error);
            }
        });
    }

    //评论的点击事件
    public void reportcomment(){

        AccountRESTFul accountRESTFul=new AccountRESTFul();
        accountRESTFul.get(new AccountRESTFul.IhttpCb() {
            @Override
            public void ok(JSONObject response) {
                Log.e("当前话题的id",Integer.toString(topicInfo.getId()));
                JSONObject jsonObject=response;
                account=ToJSonObject.jsonToBean(jsonObject.toString(),Account.class);
                Comment comment=new Comment();
                comment.setId(null);
                comment.setCommenter(account.getLogin());
                comment.setContent(diss_comment.getText().toString());
                comment.setCount(0);
                comment.setTopic(topicInfo);
                if(diss_comment.getText().toString().equals("")) {
                    Toast.makeText(CurrentTopicsActivity.this, "内容不能为空！", Toast.LENGTH_SHORT).show();
                }else{
                    JSONObject com = ToJSonObject.beanToJSONObejct(comment);
                    CommentRESTFul commentRESTFul = new CommentRESTFul();
                    commentRESTFul.postComment(com, new CommentRESTFul.MyPostCallBack() {
                        @Override
                        public void ok(JSONObject response) throws JSONException {
                            ccount = topicInfo.getDisscusscount();
                            ccount += 1;
                            topicInfo.setDisscusscount(ccount);
                            diss_comment.setText("");
                            disscuss_layout.setVisibility(View.GONE);
                            disscuss_layout1.setVisibility(View.VISIBLE);
                            Release_diss();
                        }

                        @Override
                        public void error(String error) {
                            Log.e("eee", "评论失败" + error);
                            Toast.makeText(CurrentTopicsActivity.this, "请将评论内容控制在255字数内", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

            @Override
            public void error(String error) {

            }
        });
    }

    //修改话题中的评论数
    public void Release_diss(){
        JSONObject top = ToJSonObject.beanToJSONObejct(topicInfo);
        TopicInfoRESTFul topicInfoRESTFul=new TopicInfoRESTFul();
        topicInfoRESTFul.putTopicInfo(top, new TopicInfoRESTFul.MyPostCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                Toast.makeText(CurrentTopicsActivity.this, "评论成功！", Toast.LENGTH_SHORT).show();
                disscuss.setText(ccount.toString());
                Log.e("当前评论数",Integer.toString(topicInfo.getDisscusscount()));
                initDatas();
                commentList.clear();
            }

            @Override
            public void error(String error) {
                Log.e("当前评论数","评论数失败"+error);
            }
        });
    }

    public static class SimpleStringRecyclerViewAdapter
            extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
        private final TypedValue mTypedValue = new TypedValue();
        private int mBackground;
        private List<Comment> mValues;

        public class ViewHolder extends RecyclerView.ViewHolder {
            public Comment top;
            public final View mView;
            public final CircleNetworkImageView commentimg;
            public final TextView commentName;
            public final TextView commentContent;
            public final TextView commentTime;
            public TextView comment_floor;

            public ViewHolder(View view) {
                super(view);
                mView = view;
                commentimg = (CircleNetworkImageView) view.findViewById(R.id.comment_person_image);
                commentName = (TextView) view.findViewById(R.id.comment_name);
                commentContent = (TextView) view.findViewById(R.id.comment_content);
                commentTime = (TextView) view.findViewById(R.id.comment_time);
                comment_floor = (TextView) view.findViewById(R.id.comment_floor);
            }

        }

        public Comment get(int position) {
            return mValues.get(position);
        }

        public SimpleStringRecyclerViewAdapter(Context context, List<Comment> items) {
            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
            mBackground = mTypedValue.resourceId;
            mValues = items;
        }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.topic_comment_item, parent, false);
            view.setBackgroundResource(mBackground);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder Myholder, final int position) {
            Myholder.top = mValues.get(position);
            Myholder.commentimg.setDefaultImageResId(R.mipmap.headimg);
            Myholder.commentimg.setErrorImageResId(R.mipmap.headimg_error);
            UserRESTFul userRESTFul=new UserRESTFul();
            userRESTFul.getUser(mValues.get(position).getCommenter(), new UserRESTFul.UserPOSTCallBack() {
                        @Override
                        public void ok(JSONObject response) throws JSONException {
                            userTest = ToJSonObject.jsonToBean(response.toString(), UserTest.class);
                            Myholder.commentName.setText(userTest.getnickName());
                            if (userTest.getHeadimg()!=null && !userTest.getHeadimg().equals("")){
//                                Glide.with(Myholder.commentimg.getContext())
//                                        .load(userTest.getHeadimg())
//                                        .fitCenter()
//                                        .into(Myholder.commentimg);
                                Myholder.commentimg.setImageUrl(userTest.getHeadimg(), App.imageLoader);
                            }
                            Log.e("ylj", "user" + userTest.toString());
                        }

                        @Override
                        public void error(String response) {

                        }
                    });
            Myholder.commentContent.setText(mValues.get(position).getContent());
            Myholder.commentimg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Context context = view.getContext();
                    Intent intent = new Intent(context, AddFriendActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("currentcomment",mValues.get(position).getCommenter());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });

//            Glide.with(Myholder.commentimg.getContext())
//                    .load(mValues.get(position).getCommenter())
//                    .fitCenter()
//                    .into(Myholder.commentimg);
            Log.e("position = ",(position+2)+"");
            Myholder.comment_floor.setText((position+2)+"楼");
        }

        @Override
        public int getItemCount() {
            return mValues.size();
        }
    }
    @Override
    public void onClick(View v) {
        Intent intent;
        Bundle bundle;
        InputMethodManager imm;
        switch (v.getId()) {
            case R.id.report_person_image:
                intent = new Intent(this, AddFriendActivity.class);
                bundle = new Bundle();
                bundle.putSerializable("currentopics",topicInfo.getCreater());
                intent.putExtras(bundle);
                this.startActivity(intent);
                break;
            case R.id.dianzan_layout:
                dianzanSuccess();
                break;
            case R.id.diss_button:
            case R.id.comment_layout:
                commentStart();
                break;
            case R.id.dis_release:
                /*关闭输入入法窗口*/
                imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(diss_comment.getWindowToken(),0);
                reportcomment();
                break;
            case R.id.main:
                disscuss_layout.setVisibility(View.GONE);
                disscuss_layout1.setVisibility(View.VISIBLE);
                imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                break;
            case R.id.topicimg1:
                intent = new Intent(this, BigImageDetailActivity.class);
                bundle = new Bundle();
                bundle.putSerializable("images", (Serializable) datas);
                intent.putExtras(bundle);
                intent.putExtra("position", 0);
                int[] location = new int[2];
                topicimg1.getLocationOnScreen(location);
                intent.putExtra("locationX", location[0]);//必须
                intent.putExtra("locationY", location[1]);//必须
                intent.putExtra("width", topicimg1.getWidth());//必须
                intent.putExtra("height", topicimg1.getHeight());//必须
                startActivity(intent);
                overridePendingTransition(0, 0);
                break;
            case R.id.topicimg2:
                intent = new Intent(this, BigImageDetailActivity.class);
                bundle = new Bundle();
                bundle.putSerializable("images", (Serializable) datas);
                intent.putExtras(bundle);
                intent.putExtra("position", 1);
                int[] location1 = new int[2];
                topicimg2.getLocationOnScreen(location1);
                intent.putExtra("locationX", location1[0]);//必须
                intent.putExtra("locationY", location1[1]);//必须
                intent.putExtra("width", topicimg2.getWidth());//必须
                intent.putExtra("height", topicimg2.getHeight());//必须
                startActivity(intent);
                overridePendingTransition(0, 0);
                break;
            case R.id.topicimg3:
                intent = new Intent(this, BigImageDetailActivity.class);
                bundle = new Bundle();
                bundle.putSerializable("images", (Serializable) datas);
                intent.putExtras(bundle);
                intent.putExtra("position", 2);
                int[] location2 = new int[2];
                topicimg3.getLocationOnScreen(location2);
                intent.putExtra("locationX", location2[0]);//必须
                intent.putExtra("locationY", location2[1]);//必须
                intent.putExtra("width", topicimg3.getWidth());//必须
                intent.putExtra("height", topicimg3.getHeight());//必须
                startActivity(intent);
                overridePendingTransition(0, 0);
                break;
        }
    }
    void commentStart(){
        InputMethodManager imm;
        disscuss_layout1.setVisibility(View.GONE);
        disscuss_layout.setVisibility(View.VISIBLE);
        diss_comment.setEnabled(true);
        diss_comment.setFocusable(true);
        diss_comment.setFocusableInTouchMode(true);
        diss_comment.requestFocus();
        diss_comment.requestFocusFromTouch();
        diss_comment.selectAll();
                /*打开输入法窗口*/
        imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                /*接受软键盘输入的编辑文本或其它视图*/
        imm.showSoftInput(diss_comment, InputMethodManager.RESULT_SHOWN);
                /*如果输入法打开则关闭，如果没打开则打开*/
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    //输入框取消键盘事件监听
    @Override
    public boolean dispatchTouchEvent(MotionEvent move) {
        if (move.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isShouldHideInput(view, move)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(move);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(move)) {
            return true;
        }
        return onTouchEvent(move);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
}